var app = getApp()
Page({
  onLoad: function (options) {
    var that = this
    //调用应用实例的方法获取全局数据
    app.getUserInfo(function (userInfo) {
      var url = (options.type == 'recommit') ? 'https://m.haotoufa.com/api/question/index?recommit=true':'https://m.haotoufa.com/api/question/index';
      //更新数据
      wx.request({
        url: url,
        data: {
          nickName: userInfo.nickName
        },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          if (res.data.hasRecord) {
            app.globalData.hasRecord = res.data;
            wx.redirectTo({
              url: '../already/already'
            })
          } else {
            that.setData({
              getInfo: res.data
            })
            that.setData({
              viewHidden: false
            })
          }
        }
      })
    })
  },
  data: {
    topInfo: {
      backImage: '../../images/backImage.jpg',
      mainTitle: '好头发在线自诊',
      description: '请认真填写以下内容，以便头发专家给出准确结果，谢谢。'
    },
    viewHidden: true,
    toView: 'top',
    getInfo: {},
    defaultSize: 'default',
    primarySize: 'default',
    warnSize: 'default',
    disabled: false,
    plain: false,
    loading: false,
    toast1Hidden: true,
    toast2Hidden: true,
    flag: true,
    question2: 'default',
    question13: 'default'
  },
  Change1: function (e) {
    this.setData({
      question1: e.detail.value
    })
  },
  Change2: function (e) {
    this.setData({
      question2: e.detail.value
    })
  },
  Change3: function (e) {
    this.setData({
      question3: e.detail.value
    })
  },
  Change4: function (e) {
    this.setData({
      question4: e.detail.value
    })
  },
  Change5: function (e) {
    this.setData({
      question5: e.detail.value
    })
  },
  Change6: function (e) {
    this.setData({
      question6: e.detail.value
    })
  },
  Change7: function (e) {
    this.setData({
      question7: e.detail.value
    })
  },
  Change8: function (e) {
    this.setData({
      question8: e.detail.value
    })
  },
  Change9: function (e) {
    this.setData({
      question9: e.detail.value
    })
  },
  Change10: function (e) {
    this.setData({
      question10: e.detail.value
    })
  },
  Change11: function (e) {
    this.setData({
      question11: e.detail.value
    })
  },
  Change12: function (e) {
    this.setData({
      question12: e.detail.value
    })
  },
  Change13: function (e) {
    this.setData({
      question13: e.detail.value
    })
  },
  Change14: function (e) {
    this.setData({
      question14: e.detail.value
    })
  },
  bindMarryChange: function (e) {
    this.setData({
      marry: e.detail.value
    })
  },
  placeChange: function (e) {
    this.setData({
      place: e.detail.value
    })
  },
  workChange: function (e) {
    this.setData({
      work: e.detail.value
    })
  },
  lifeStyleChange: function (e) {
    this.setData({
      lifeStyle: e.detail.value
    })
  },
  AlopeciaDateChange: function (e) {
    this.setData({
      AlopeciaDate: e.detail.value
    })
  },
  cureCostChange: function (e) {
    this.setData({
      cureCost: e.detail.value
    })
  },
  hairConditionChange: function (e) {
    this.setData({
      hairCondition: e.detail.value
    })
  },
  sleepRankChange: function (e) {
    this.setData({
      sleepRank: e.detail.value
    })
  },
  eatHabitChange: function (e) {
    this.setData({
      eatHabit: e.detail.value
    })
  },
  lostRankChange: function (e) {
    this.setData({
      lostRank: e.detail.value
    })
  },
  currentHairChange: function (e) {
    this.setData({
      currentHair: e.detail.value
    })
  },
  inheritanceChange: function (e) {
    this.setData({
      inheritance: e.detail.value
    })
  },
  ceilPhoneChange: function (e) {
    this.setData({
      ceilPhone: e.detail.value
    })
  },
  modalChange2: function (e) {
    this.setData({
      toast2Hidden: true
    })
  },
  upper: function (e) {
    console.log(e)
  },
  lower: function (e) {
    console.log(e)
  },
  scroll: function (e) {
    console.log(e)
  },
  scrollToTop: function (e) {
    this.setAction({
      scrollTop: 0
    })
  },
  tap: function (e) {
    for (var i = 0; i < order.length; ++i) {
      if (order[i] === this.data.toView) {
        this.setData({
          toView: order[i + 1],
          scrollTop: (i + 1) * 200
        })
        break
      }
    }
  },
  tapMove: function (e) {
    this.setData({
      scrollTop: this.data.scrollTop + 10
    })
  },
  ajaxtest: function (e) {
    let data_use = {
      question: {
        question1: this.data.question1,
        question2: this.data.question2,
        question3: this.data.question3,
        question4: this.data.question4,
        question5: this.data.question5,
        question6: this.data.question6,
        question7: this.data.question7,
        question8: this.data.question8,
        question9: this.data.question9,
        question10: this.data.question10,
        question11: this.data.question11,
        question12: this.data.question12,
        question13: this.data.question13
      },
      nickName: app.globalData.userInfo.nickName
    }
    this.data.flag = true;
    var keyGroup = data_use.question;
    var key = Object.keys(keyGroup);
    for (let key in keyGroup) {
      if (keyGroup[key] == undefined) {
        this.setData({
          toast2Hidden: false,
          flag: false
        })
        break;
      }
    }
    if (this.data.flag) {
      var that = this;
      wx.request({
        url: 'https://m.haotoufa.com/api/question/store',
        method: 'POST',
        data: data_use,
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          that.setData({
            toast1Hidden: false
          })
          app.globalData.consultData = res.data;
          setTimeout(function () {
            that.setData({
              toast1Hidden: true
            })
            wx.navigateTo({
              url: '../consult/consult'
            })
          }, 500)
        }
      })
    }
  }
})